<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common/commonInclude.jsp"%>
<!DOCTYPE html>
<html class="no-js">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>REST API</title>
  <meta name="description" content="这是一个 index 页面">
  <meta name="keywords" content="index">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="icon" type="image/png" href="<%=basePath %>AmazeUI-2.1.0/assets/i/favicon.png">
  <link rel="apple-touch-icon-precomposed" href="<%=basePath %>AmazeUI-2.1.0/assets/i/app-icon72x72@2x.png">
  <meta name="apple-mobile-web-app-title" content="Amaze UI" />
  <link rel="stylesheet" href="<%=basePath %>AmazeUI-2.1.0/assets/css/amazeui.min.css"/>
  <link rel="stylesheet" href="<%=basePath %>AmazeUI-2.1.0/assets/css/admin.css">
  <link rel="stylesheet" href="<%=basePath %>assets/css/oauth_error.css"/>
  <script type="text/javascript" src="<%=basePath %>assets/js/jquery-1.11.1.js"></script>
  <script>
  	function changeSecret(){
		if(window.confirm("确定更新secret？这会导致正在使用的secret发生异常！")) {
			$.ajax({
				url : '../changeSecret',
				dataType : 'json',
				success : function (data) {
					if (data.ret == 0) {
						$('#secret').html(data.data);
					} else {
						alert(data.data);
					}
				}
			})
		}
  	}
  	function changePassword(){
		var password = window.prompt("请输入新密码，密码不能为空！","");
		$.ajax({
			url : '../changePassword',
			data : {password : password},
			dataType : 'json',
			success : function (data) {
				if (data.ret == 0) {
					alert("密码修改成功！");
				} else {
					alert(data.data);
				}
			}
		})
  	}
  	function getCode(){
		$.ajax({
			url : '../oauth2/authorize',
			data : {
				state : $('#state').val(),
				appid : $('#appid').html(),
				secret : $('#secret').html(),
				response_type : $('#response_type').val()
			},
			dataType : 'json',
			success : function (data) {
				$('#code').val(data.code);
			}
		})
  	}
  	function getToken(){
		$.ajax({
			url : '../oauth2/access_token',
			data : {
				appid : $('#appid').html() , 
				secret : $('#secret').html(),
				code : $('#code').val(),
				grant_type : 'openapi'
			},
			dataType : 'json',
			success : function (data) {
				$('#token').html(JSON.stringify(data).replace(',"',',\n"').replace(',"',',\n"').replace('{','{\n').replace('}','\n}'));
			}
		})
  	}
  </script>
</head>
<body style="text-align: center;font-size: 18px;">
	<br/>
	<h2>${client.name }
	<button onclick="changePassword()" class="am-btn am-btn-primary am-round am-btn-xs">更改密码</button>
	</h2>
	<hr></hr>
	<div>
		<strong>appid：&nbsp;&nbsp;&nbsp;&nbsp;</strong>
		<span style="width: 400px;display: inline-block;text-align: left" id="appid">${client.cid }</span>
	</div>
	<div>
		<strong>secret：&nbsp;&nbsp;&nbsp;&nbsp;</strong>
		<span id="secret" style="width: 346px;display: inline-block;text-align: left">${client.secret }</span>
		<button onclick="changeSecret()" class="am-btn am-btn-primary  am-btn-xs">更新</button>
	</div><br/>
	<hr></hr><br/>
	<div>
		<input type="text" id="response_type" placeholder="response_type"/><br/><br/>
		<input type="text" id="state" placeholder="state"/><br/><br/>
		<button onclick="getCode()" class="am-btn am-btn-primary  am-btn-xs">获取code</button>
	</div><br/>
	<hr></hr>
	<div>
		<input type="text" id="code" placeholder="code"/>
		<button onclick="getToken()" class="am-btn am-btn-primary  am-btn-xs">获取Token</button><br/><br/>
		<div><pre style="width: 403px;height: 137px;text-align: left;display: inline-block;" id="token"></pre></div>
	</div>
</body>
</html>